package com.tulele.vtool.task.async;

import com.alibaba.fastjson.JSONObject;
import com.tulele.vtool.bus.mapper.log.PushDetailLogMapper;
import com.tulele.vtool.bus.mapper.log.PushHeadLogMapper;
import com.tulele.vtool.bus.model.UserBrandInfo;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.log.PushDetailLog;
import com.tulele.vtool.bus.model.log.PushHeadLog;
import com.tulele.vtool.bus.model.thirdparty.WxPushUserInfo;
import com.tulele.vtool.bus.service.*;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.model.UserGroupOpenId;
import com.tulele.vtool.util.*;
import com.tulele.vtool.wechat.service.WeixinService;
import com.tulele.vtool.wechat.service.WeixinServiceDL;
import com.tulele.vtool.wechat.service.WeixinServiceMap;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Component
@Slf4j
public class UserOperateDataPushAsyncTask {

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private ObjectFactory<WeixinServiceDL> objectFactory;

    @Autowired
    private SysGzhService sysGzhService;

    @Autowired
    private IArticleProfessionalService articleProfessionalService;

    @Autowired
    private IUserService userService;

    @Autowired
    private PushDetailLogMapper pushDetailLogMapper;


    @Value("${NotInvolvedInActivities}")
    private String notInvolvedInActivities;//不参与活动的 公众号

    @Value("${RecommendedCourses}")
    private String recommendedCourses;//推课程的 公众号

    @Autowired
    private UserGroupGzhService userGroupGzhService;

    //新年
    @Async("taskExecutor")
    public void execute20210208(String redisKey, UserInfo userInfo, Integer headId, String profile) throws Exception {

        Integer ret = 2;

        WxPushUserInfo wxPushUserInfo = new WxPushUserInfo();
        wxPushUserInfo.setOpenId(userInfo.getOpenId());

        UserGroupOpenId ugoId = userGroupGzhService.getUserOpenId(profile, userInfo.getUserId());
        if (ugoId != null) {
            wxPushUserInfo.setOpenId(ugoId.getOpenId());
        }

        wxPushUserInfo.setUserId(userInfo.getUserId());

        if (wxPushUserInfo != null) {
            PlatformSysInfo sysGzh = sysGzhService.getPlatformSysInfo(profile);
            //20200530 循环推送
            Integer number = 1;
            Object pushNumberObj = redisCacheUtil.getCacheObject("push1710Number" + wxPushUserInfo.getUserId());
            if (pushNumberObj != null) {
                number = (Integer) pushNumberObj;
                if (number > 3) {
                    number = 1;
                }
            }
            redisCacheUtil.setCacheObject("push1710Number" + wxPushUserInfo.getUserId(), number + 1, 100000);
            //20200530 循环推送

            WxMpKefuMessage.WxArticle wxArticle = null;
            WxMpTemplateMessage templateMessage = null;
            String kefuMsg = "";

            String notVipMsg = "\uD83D\uDE0D 2022年，您的新订单来了\uD83D\uDCB0\n\n" +
                    "➡ <a href=\"weixin://bizmsgmenu?msgmenucontent=客户&msgmenuid=+++\"> [点击这里]立即接收>>></a>";


            if (number.equals(1)) {
                kefuMsg =
                        userInfo.getNickName() + " ，您有9个VIP专属权益等待领取~~~\n\n" +
                        "\uD83D\uDCF2 <a href=\"weixin://bizmsgmenu?msgmenucontent=客户&msgmenuid=+++\">开启线上获客，找到你的潜在意向客户</a>\n\n" +
                        "\uD83D\uDD25 <a href=\"" + sysGzh.getOpenMemberMenuUrl() + "?mt=1&cardBag=1\">回复【 8 】点击领取>>></a>\n\n";

            } else if (number.equals(2)) {

                if (!userInfo.getVipStatus().equals(1)) {
                    kefuMsg =
                            "\uD83D\uDE0D " + userInfo.getNickName() + "老师，全网最新线上低成本获客平台回馈新老客户\n\n" +
                            "每天10分钟，发10次圈\n\n" +
                            "群量获客，坚持每天分享300个群\n\n" +
                            "一定有意外和惊喜！！！\n\n" +
                            "\uD83D\uDC49 <a href=\"" + sysGzh.getOpenMemberMenuUrl() + "?mt=1\">请了解优惠活动详情>></a>";
                } else {
                    kefuMsg = notVipMsg;
                }

            } else {

                if (!userInfo.getVipStatus().equals(1)) {
                    kefuMsg =
                            "\uD83D\uDCB0分享文章 邀请好友还能赚钱？\n\n" +
                            "这种好事我早就参与了！\n\n" +
                            "➡ <a href=\"weixin://bizmsgmenu?msgmenucontent=邀请有奖&msgmenuid=+++\"> 你也一起来>>></a>";
                } else {
                    kefuMsg = notVipMsg;
                }
            }

            WxMpKefuMessage message = null;
            if (!kefuMsg.equals("")) {
                if (userInfo.getPhoneModel().equals(SysConstant.IOS)) {
                    kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                }
                message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
            } else if (wxArticle != null) {
                message = WxMpKefuMessage.NEWS().addArticle(wxArticle).toUser(wxPushUserInfo.getOpenId()).build();
            }

            String detailLog = "";
            try {

                WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
                if (weixinService == null) {
                    weixinService = objectFactory.getObject();
                    weixinService.reSetConfigStorage(profile);
                    WeixinServiceMap.putWeixinService(profile, weixinService);
                }

                if (message != null) {
                    weixinService.getKefuService().sendKefuMessage(message);
                }

                if (templateMessage != null) {
                    weixinService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                }

                ret = 0;
                detailLog = "成功";
            } catch (WxErrorException e) {
                ret = 2;
                detailLog = e.getMessage();
            }

            PushDetailLog pushDetailLog = new PushDetailLog();
            pushDetailLog.setGzhCode(profile);
            pushDetailLog.setUserId(wxPushUserInfo.getUserId());
            pushDetailLog.setMsgType(17);  //17点的推送
            pushDetailLog.setPushTime(new Date());
            pushDetailLog.setHeadId(headId);
            pushDetailLog.setResult(ret);
            pushDetailLog.setMsg(detailLog);
            pushDetailLogMapper.addPushDetailLog(pushDetailLog);
        }
    }

    //年末修改前
    @Async("taskExecutor")
    public void execute20211130(String redisKey, UserInfo userInfo, Integer headId, String profile) throws Exception {

        Integer ret = 2;

        WxPushUserInfo wxPushUserInfo = new WxPushUserInfo();
        wxPushUserInfo.setOpenId(userInfo.getOpenId());

        UserGroupOpenId ugoId = userGroupGzhService.getUserOpenId(profile, userInfo.getUserId());
        if (ugoId != null) {
            wxPushUserInfo.setOpenId(ugoId.getOpenId());
        }

        wxPushUserInfo.setUserId(userInfo.getUserId());

        if (wxPushUserInfo != null) {
            PlatformSysInfo sysGzh = sysGzhService.getPlatformSysInfo(profile);
            //20200530 循环推送
            Integer number = 1;
            Object pushNumberObj = redisCacheUtil.getCacheObject("push1710Number" + wxPushUserInfo.getUserId());
            if (pushNumberObj != null) {
                number = (Integer) pushNumberObj;
                if (number > 4) {
                    number = 1;
                }
            }
            redisCacheUtil.setCacheObject("push1710Number" + wxPushUserInfo.getUserId(), number + 1, 100000);
            //20200530 循环推送

            WxMpKefuMessage.WxArticle wxArticle = null;
            WxMpTemplateMessage templateMessage = null;
            String kefuMsg = "";

            String notVipMsg = "@" + userInfo.getNickName() + "，您已获得高级推广权限~\n\n" +
                    "\uD83D\uDE18 只要您发了文章~有人通过你的文章关注开通会员，您会收到一笔奖励金~\n\n" +
                    "\uD83D\uDC49 <a href=\"weixin://bizmsgmenu?msgmenucontent=获取爆文&msgmenuid=+++\">赶紧发文推广拿奖金吧>></a>\n\n" +
                    "\uD83D\uDCB0 <a href=\"weixin://bizmsgmenu?msgmenucontent=邀请有奖&msgmenuid=+++\">点击查看我的奖励金>></a>";


            if (number.equals(1)) {

                kefuMsg = "[加油]@" + userInfo.getNickName() + ",有客户一直看您的文章！！\n\n" +
                        "\uD83D\uDC9D 再不联系客户跑了！\n\n" +
                        "\uD83D\uDC49 <a href=\"weixin://bizmsgmenu?msgmenucontent=客户&msgmenuid=+++\">赶快回复【 1 】点击查看~</a>";

            } else if (number.equals(2)) {

                if (!userInfo.getVipStatus().equals(1)) {
                    kefuMsg =
                            "\uD83D\uDC8E" + userInfo.getNickName() + "，您有9个VIP专属权益等待领取~~~\n\n" +
                                    "\uD83D\uDE0D 开启客户追踪，找到你的潜在客户圈\n\n" +
                                    "\uD83D\uDC49 <a href=\"" + sysGzh.getOpenMemberMenuUrl() + "\">回复【 8 】点击领取>>></a>";
                } else {
                    kefuMsg = notVipMsg;
                }

            } else if (number.equals(3)) {

                if (!userInfo.getVipStatus().equals(1)) {
                    kefuMsg =
                            "@" + userInfo.getNickName() + "，精准6大引流途径， 引流裂变如此简单！\n\n" +
                                    "\uD83D\uDE0D 抖音视频插入广告和名片！\n\n" +
                                    "\uD83D\uDC49 <a href=\"" + sysGzh.getOpenMemberMenuUrl() + "\">赶紧开通体验一下\uD83C\uDF00>>></a>";
                } else {
                    kefuMsg = notVipMsg;
                }

                if(profile.equals("prodhyxt") ){
                    kefuMsg = "[哇] 恭喜您获得新体验：护眼素材库\n\n" +
                            "\uD83C\uDF89 您的发圈神器，图文下载，一键搞定\n\n" +
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxb0f54660c25b84ad\" data-miniprogram-path=\"/pages/home/home\" href=\"\">点击免费体验>>></a>\n"+
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxb0f54660c25b84ad\" data-miniprogram-path=\"/pages/home/home\" href=\"\">上千护眼素材>>></a>";
                }

                if( profile.equals("prodshjj")){
                    kefuMsg = "[哇] 恭喜您获得新体验：私护素材库\n\n" +
                            "\uD83C\uDF89 您的发圈神器，图文下载，一键搞定\n\n" +
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxdc3df6682cf2d1f0\" data-miniprogram-path=\"/pages/home/home\" href=\"\">点击免费体验>>></a>\n"+
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxdc3df6682cf2d1f0\" data-miniprogram-path=\"/pages/home/home\" href=\"\">上千私护素材>>></a>";
                }

                if( profile.equals("prodbkwz")){
                    kefuMsg = "[哇] 恭喜您获得新体验：鼻康素材库\n\n" +
                            "\uD83C\uDF89 您的发圈神器，图文下载，一键搞定\n\n" +
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxd4c8d3462a815c5a\" data-miniprogram-path=\"/pages/home/home\" href=\"\">点击免费体验>>></a>\n"+
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxd4c8d3462a815c5a\" data-miniprogram-path=\"/pages/home/home\" href=\"\">上千鼻康素材>>></a>";
                }

            }else {

                kefuMsg = "@" + userInfo.getNickName() + ",有客户正在等你回复，确定不聊一聊？\n\n" +
                        "✌ 客户正关注你呢！\n\n" +
                        "\uD83D\uDC49 <a href=\"weixin://bizmsgmenu?msgmenucontent=客户&msgmenuid=+++\">回复【 1 】马上联系他>>></a>";

            }

            WxMpKefuMessage message = null;
            if (!kefuMsg.equals("")) {
                if (userInfo.getPhoneModel().equals(SysConstant.IOS)) {
                    kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                }
                message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
            } else if (wxArticle != null) {
                message = WxMpKefuMessage.NEWS().addArticle(wxArticle).toUser(wxPushUserInfo.getOpenId()).build();
            }

            String detailLog = "";
            try {

                WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
                if (weixinService == null) {
                    weixinService = objectFactory.getObject();
                    weixinService.reSetConfigStorage(profile);
                    WeixinServiceMap.putWeixinService(profile, weixinService);
                }

                if (message != null) {
                    weixinService.getKefuService().sendKefuMessage(message);
                }

                if (templateMessage != null) {
                    weixinService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                }

                ret = 0;
                detailLog = "成功";
            } catch (WxErrorException e) {
                ret = 2;
                detailLog = e.getMessage();
            }

            PushDetailLog pushDetailLog = new PushDetailLog();
            pushDetailLog.setGzhCode(profile);
            pushDetailLog.setUserId(wxPushUserInfo.getUserId());
            pushDetailLog.setMsgType(17);  //17点的推送
            pushDetailLog.setPushTime(new Date());
            pushDetailLog.setHeadId(headId);
            pushDetailLog.setResult(ret);
            pushDetailLog.setMsg(detailLog);
            pushDetailLogMapper.addPushDetailLog(pushDetailLog);
        }
    }

    @Async("taskExecutor")
    public void executeProfessional(String redisKey, UserInfo userInfo, Integer headId, String profile) throws Exception {

        Integer ret = 2;

        WxPushUserInfo wxPushUserInfo = new WxPushUserInfo();
        wxPushUserInfo.setOpenId(userInfo.getOpenId());

        UserGroupOpenId ugoId = userGroupGzhService.getUserOpenId(profile, userInfo.getUserId());
        if (ugoId != null) {
            wxPushUserInfo.setOpenId(ugoId.getOpenId());
        }
        wxPushUserInfo.setUserId(userInfo.getUserId());

        if (wxPushUserInfo != null) {

            PlatformSysInfo sysGzh = sysGzhService.getPlatformSysInfo(profile);
            //20200530 循环推送
            Integer number = 1;
            Object pushNumberObj = redisCacheUtil.getCacheObject("push1710Number" + wxPushUserInfo.getUserId());
            if (pushNumberObj != null) {
                number = (Integer) pushNumberObj;
                if (number > 4) {
                    number = 1;
                }
            }
            redisCacheUtil.setCacheObject("push1710Number" + wxPushUserInfo.getUserId(), number + 1, 100000);
            //20200530 循环推送

            WxMpKefuMessage.WxArticle wxArticle = null;
            WxMpTemplateMessage templateMessage = null;
            String kefuMsg = "";

            String notVipMsg = "@" + userInfo.getNickName() + "，您已获得高级推广权限~\n\n" +
                    "\uD83D\uDE18 只要您发了文章~有人通过你的文章关注开通会员，您会收到一笔奖励金~\n\n" +
                    "\uD83D\uDC49 <a href=\"weixin://bizmsgmenu?msgmenucontent=获取爆文&msgmenuid=+++\">赶紧发文推广拿奖金吧>></a>\n\n" +
                    "\uD83D\uDCB0 <a href=\"weixin://bizmsgmenu?msgmenucontent=邀请有奖&msgmenuid=+++\">点击查看我的奖励金>></a>";

            if (number.equals(1)) {

                kefuMsg = "[加油]@" + userInfo.getNickName() + ",有客户一直看您文章，而您还没查看哦\n\n" +
                        "\uD83D\uDC96 说明朋友圈还是有您的潜在客户的\n\n" +
                        "\uD83D\uDC49 <a href=\"weixin://bizmsgmenu?msgmenucontent=客户&msgmenuid=+++\">赶快点击查看拉~</a>";

            } else if (number.equals(2)) {

                if (!userInfo.getVipStatus().equals(1)) {
                    kefuMsg = "\uD83D\uDC8E" + userInfo.getNickName() + "，您有9个VIP专属权益等待领取~~~\n\n" +
                                    "\uD83D\uDE0D 开启客户追踪，找到你的潜在客户圈\n\n" +
                                    "\uD83D\uDC49 <a href=\"" + sysGzh.getOpenMemberMenuUrl() + "\">点击领取>>></a>";
                } else {
                    kefuMsg = notVipMsg;
                }

            } else if (number.equals(3)) {

                if (!userInfo.getVipStatus().equals(1)) {
                    kefuMsg =
                            "@" + userInfo.getNickName() + "，精准6大引流途径， 引流裂变如此简单！\n\n" +
                                    "\uD83D\uDE0D 抖音视频插入广告和名片！\n\n" +
                                    "\uD83D\uDC49 <a href=\"" + sysGzh.getOpenMemberMenuUrl() + "\">赶紧开通体验一下\uD83C\uDF00>>></a>";
                } else {
                    kefuMsg = notVipMsg;
                }

                if(profile.equals("prodhyxt") ){
                    kefuMsg = "[哇] 恭喜您获得新体验：护眼素材库\n\n" +
                            "\uD83C\uDF89 您的发圈神器，图文下载，一键搞定\n\n" +
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxb0f54660c25b84ad\" data-miniprogram-path=\"/pages/home/home\" href=\"\">点击免费体验>>></a>\n"+
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxb0f54660c25b84ad\" data-miniprogram-path=\"/pages/home/home\" href=\"\">上千护眼素材>>></a>";
                }

                if( profile.equals("prodshjj")){
                    kefuMsg = "[哇] 恭喜您获得新体验：私护素材库\n\n" +
                            "\uD83C\uDF89 您的发圈神器，图文下载，一键搞定\n\n" +
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxdc3df6682cf2d1f0\" data-miniprogram-path=\"/pages/home/home\" href=\"\">点击免费体验>>></a>\n"+
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxdc3df6682cf2d1f0\" data-miniprogram-path=\"/pages/home/home\" href=\"\">上千私护素材>>></a>";
                }

                if( profile.equals("prodbkwz")){
                    kefuMsg = "[哇] 恭喜您获得新体验：鼻康素材库\n\n" +
                            "\uD83C\uDF89 您的发圈神器，图文下载，一键搞定\n\n" +
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxd4c8d3462a815c5a\" data-miniprogram-path=\"/pages/home/home\" href=\"\">点击免费体验>>></a>\n"+
                            "\uD83D\uDC49 <a data-miniprogram-appid=\"wxd4c8d3462a815c5a\" data-miniprogram-path=\"/pages/home/home\" href=\"\">上千鼻康素材>>></a>";
                }

            }else {

                kefuMsg = "@" + userInfo.getNickName() + ",有客户正在等你回复，确定不聊一聊？\n\n" +
                        "✌ 客户正关注你呢！\n\n" +
                        "\uD83D\uDC49 <a href=\"weixin://bizmsgmenu?msgmenucontent=客户&msgmenuid=+++\">[点击这里]马上联系>>></a>";

            }

            WxMpKefuMessage message = null;
            if (!kefuMsg.equals("")) {
                if (userInfo.getPhoneModel().equals(SysConstant.IOS)) {
                    kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                }
                message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
            } else if (wxArticle != null) {
                message = WxMpKefuMessage.NEWS().addArticle(wxArticle).toUser(wxPushUserInfo.getOpenId()).build();
            }

            String detailLog = "";
            try {

                WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
                if (weixinService == null) {
                    weixinService = objectFactory.getObject();
                    weixinService.reSetConfigStorage(profile);
                    WeixinServiceMap.putWeixinService(profile, weixinService);
                }

                if (message != null) {
                    weixinService.getKefuService().sendKefuMessage(message);
                }

                if (templateMessage != null) {
                    weixinService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                }

                ret = 0;
                detailLog = "成功";
            } catch (WxErrorException e) {
                ret = 2;
                detailLog = e.getMessage();
            }

            PushDetailLog pushDetailLog = new PushDetailLog();
            pushDetailLog.setGzhCode(profile);
            pushDetailLog.setUserId(wxPushUserInfo.getUserId());
            pushDetailLog.setMsgType(17);  //17点的推送
            pushDetailLog.setPushTime(new Date());
            pushDetailLog.setHeadId(headId);
            pushDetailLog.setResult(ret);
            pushDetailLog.setMsg(detailLog);
            pushDetailLogMapper.addPushDetailLog(pushDetailLog);

        }

    }
}
